package com.hbqyml.web.controller.common;

import com.hbqyml.web.core.config.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : houyutong
 * @date : 2021/9/25 17:18
 */
@RestController
public class UploadController {

    @Autowired
    AliOssUtil aliOssUtill;

    @PostMapping("/upload")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws IOException {
//        System.out.println("调用了上传接口");
        String s = aliOssUtill.uploadObject2OSS(file);
        Map<String, Object> map = new HashMap<>();
        map.put("file", s);
        return map;
    }


    /**
     * 上传文件
     * @Author houyutong
     * @Date 2021/9/18 18:15
     * @param file
     * @return java.lang.String
     */
    /*@PostMapping("/upload")
    @ResponseBody
    public Map<String,Object> upload(@RequestParam("file") MultipartFile file) throws InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException {        Map<String, Object> map = new HashMap<>();
        long size = file.getSize();
        if (size > 1024 * 1024 * 1024) {
            map.put("code", 500);
            map.put("data", "超过最大上传限制");
            return map;
        }
        //得到文件流
        InputStream is= file.getInputStream();
        //文件名
        String fileName = file.getOriginalFilename();
        String newFileName= UUID.randomUUID().toString().replaceAll("-","")+"."+ StringUtils.substringAfterLast(fileName,".");
        //类型
        String contentType = file.getContentType();
        String url=minioUtil.uploadObject(is,newFileName,contentType);
        System.out.println(url);

        map.put("file", url);
        return map;
    }*/

    /**
     * 下载minio服务的文件
     * @Author houyutong
     * @Date 2021/9/18 18:15
     * @param filename
     * @param response
     * @return void
     */
    /*@GetMapping("/minio/download")
    public void download(String filename, HttpServletResponse response) {
        try {
            InputStream fileInputStream = minioUtil.getObject(filename);
            response.setHeader("Content-Disposition", "attachment;filename=" + "test.jpg");
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }*/

}
